#cmake file for examples
#author:guyadong
#created:2020/04/02
# dtalk demo 程序

#########################
# 设置项目属性
function (define_example_target target mqlib impldef)
  if(NOT TARGET ${target})
    return ()
  endif()
  #with_mt_if_msvc(${target})
  # 因为大量使用模板,导致代码增大,MSVC下要用/bigobj选项,否则会报错:error C1128
  # GNU compiler 要用 -Wa,-mbig-obj 选项
  target_compile_options(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/bigobj> 
      $<$<AND:$<CXX_COMPILER_ID:GNU>,$<BOOL:${GNU_BIG_OBJ_FLAG_ENABLE}>>:-Wa,-mbig-obj>) 
  set_target_properties (${target}  PROPERTIES FOLDER examples )
  target_compile_definitions(${target} PRIVATE ${impldef})
  target_link_options(${target} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/ignore:4099> )
  target_link_libraries(${target} dtalk ${mqlib} $<$<BOOL:${MINGW}>:iphlpapi>)
  unset(_link_libraries)
endfunction()

# 基于REDIS的demo
find_package(cpp_redis QUIET)

# 获取common_source_cpp的文件夹
get_target_property(_common_source_includes common_source_cpp::common_source_cpp INTERFACE_INCLUDE_DIRECTORIES)
list(GET _common_source_includes 0 _common_source_location)
MESSAGE(STATUS _common_source_location=${_common_source_location})

if( TARGET cpp_redis::cpp_redis )
  add_executable(redis_demo  dtalk_demo.cpp ${_common_source_location}/if_utilits.cpp)
  define_example_target(redis_demo cpp_redis::cpp_redis _REDIS_IMPL_)
endif()

# 基于MQTT的demo
find_package(PahoMqttCpp QUIET)

if(TARGET PahoMqttCpp::paho-mqttpp3-static)
  message(STATUS "target PahoMqttCpp::paho-mqttpp3-static FOUND")
else()
  message(STATUS "target PahoMqttCpp::paho-mqttpp3-static NOT FOUND") 
endif()

if( TARGET PahoMqttCpp::paho-mqttpp3-static)
  add_executable(mqtt_demo  dtalk_demo.cpp ${_common_source_location}/if_utilits.cpp)
  define_example_target(mqtt_demo PahoMqttCpp::paho-mqttpp3-static _MQTT_IMPL_)
endif()
#####################

